package com.suxin.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author Tang
 * @classname SemaphoreTest
 * @description [  ]
 * @date 2021/9/16 22:03
 */
@Slf4j
public class SemaphoreTest {

    private static Semaphore semaphore = new Semaphore(10);

    public static void main(String[] args) throws Exception {
        IntStream.range(1, 20).forEach(item -> {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    log.info("休眠 start..");
                    TimeUnit.SECONDS.sleep(2);
                    log.info("休眠 end..");
                    semaphore.release();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "thread-" + item).start();
        });
        TimeUnit.SECONDS.sleep(15);
    }

}